package com.xzbd.gef.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.CompoundSnapToHelper;
import org.eclipse.gef.Request;
import org.eclipse.gef.RequestConstants;
import org.eclipse.gef.SnapToGeometry;
import org.eclipse.gef.SnapToGrid;
import org.eclipse.gef.SnapToGuides;
import org.eclipse.gef.SnapToHelper;
import org.eclipse.gef.editparts.AbstractGraphicalEditPart;
import org.eclipse.gef.rulers.RulerProvider;
import org.eclipse.ui.IPageLayout;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;

import com.xzbd.listeners.PropListener;

public class AppAbstractEditPart  extends AbstractGraphicalEditPart implements PropertyChangeListener {
	public void activate() {
		super.activate();
//		((RootNode) getModel()).addPropertyChangeListener(this);
	}

	public void deactivate() {
		super.deactivate();
//		((RootNode) getModel()).removePropertyChangeListener(this);
	}

	@Override
	public void propertyChange(PropertyChangeEvent evt) {
		final String propertyName = evt.getPropertyName();
		if (propertyName.equals(PropListener.PROPERTY_LAYOUT))
			refreshVisuals();
		else if (propertyName.equals(PropListener.PROPERTY_ADD)) {
			System.out.println(this.getClass().toString());;
			refreshChildren(); 
		}
		else if (propertyName.equals(PropListener.PROPERTY_REMOVE)) {
			refreshVisuals();
			refreshChildren();
		} else if (propertyName.equals(PropListener.PROPERTY_RENAME))
			refreshVisuals();
//		else if (propertyName.equals(Appearance.APPEARANCE_FONTSIZE))
//			refreshVisuals();
//		else if (propertyName.equals(Appearance.APPEARANCE_FONTCOLOR))
//			refreshVisuals();
//		else if (propertyName.equals(Appearance.APPEARANCE_BACKGROUNDCOLOR))
//			refreshVisuals();
//		else if (propertyName.equals(Appearance.APPEARANCE_BORDERCOLOR))
//			refreshVisuals();
//		else if (propertyName.equals(Appearance.APPEARANCE_ICON))
//			refreshVisuals();
//		else if (propertyName.equals(Constants.SELECT_STATUE_CHANGE))
//			this.setSelected((int) evt.getNewValue());
	}

	@Override
	protected IFigure createFigure() {
		return null;
	}

	@Override
	protected void createEditPolicies() {
		// TODO Auto-generated method stub
		// installEditPolicy(EditPolicy.DIRECT_EDIT_ROLE, new NodeDirectEditPolicy());
	}

	@SuppressWarnings("all")
	public Object getAdapter(Class adapter) {
//		this.getChildren().stream().forEach(s -> {
//			int sx = ((AppAbstractEditPart) s).getFigure().getBounds().x;
//			int sy = ((AppAbstractEditPart) s).getFigure().getBounds().y;
//			if (sx < 0) {
//				Rectangle rectangle = ((AppAbstractEditPart) s).getFigure().getBounds();
//				this.getChildren().stream().forEach(c -> {
//					int cx = ((AppAbstractEditPart) c).getFigure().getBounds().x;
//					Rectangle rectanglec = ((AppAbstractEditPart) c).getFigure().getBounds();
//					((Node) ((AppAbstractEditPart) c).getModel())
//							.setLayout(new Rectangle(cx - sx, rectanglec.y, rectanglec.width, rectanglec.height));
//				});
//			}
//			if (sy < 0) {
//				Rectangle rectangle = ((AppAbstractEditPart) s).getFigure().getBounds();
//				this.getChildren().stream().forEach(c -> {
//					int cy = ((AppAbstractEditPart) c).getFigure().getBounds().y;
//					Rectangle rectanglec = ((AppAbstractEditPart) c).getFigure().getBounds();
//					((Node) ((AppAbstractEditPart) c).getModel())
//							.setLayout(new Rectangle(rectanglec.x, cy - sy, rectanglec.width, rectanglec.height));
//				});
//			}
//		});
		if (adapter == SnapToHelper.class) {
			List snapStrategies = new ArrayList();
			Boolean val = (Boolean) getViewer().getProperty(RulerProvider.PROPERTY_RULER_VISIBILITY);
			if (val != null && val.booleanValue())
				snapStrategies.add(new SnapToGuides(this));
			val = (Boolean) getViewer().getProperty(SnapToGeometry.PROPERTY_SNAP_ENABLED);
			if (val != null && val.booleanValue())
				snapStrategies.add(new SnapToGeometry(this));
			val = (Boolean) getViewer().getProperty(SnapToGrid.PROPERTY_GRID_ENABLED);
			if (val != null && val.booleanValue())
				snapStrategies.add(new SnapToGrid(this));

			if (snapStrategies.size() == 0)
				return null;
			if (snapStrategies.size() == 1)
				return (SnapToHelper) snapStrategies.get(0);

			SnapToHelper ss[] = new SnapToHelper[snapStrategies.size()];
			for (int i = 0; i < snapStrategies.size(); i++)
				ss[i] = (SnapToHelper) snapStrategies.get(i);
			return new CompoundSnapToHelper(ss);
		}
		return super.getAdapter(adapter);
	}

	public void performRequest(Request req) {
		if (req.getType().equals(RequestConstants.REQ_OPEN)) {
			try {
				IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
				page.showView(IPageLayout.ID_PROP_SHEET);
			} catch (PartInitException e) {
				e.printStackTrace();
			}
		}
	}

	
	public void refreshVisuals() {

	}

}
